date命令

        date 在脚本中用得很多,那必须得学会这个命令,下面是 date 的几种用法:

  1. 输出四位的年、月、日,格式 2017.04.27
1
[root@192 sbin]# date +%Y-%m-%d

  1. 输出两位的年、月、日,格式17-04-27
1
[root@192 sbin]# date +%y-%m-%d

  1. 另外一种输出年、月、日的方式
1
[root@192 sbin]# date +%F

  1. 输出时间,格式 02:05:31
1
[root@192 sbin]# date +%H:%M:%S

  1. 另外一种输出时间的方式,格式同 4
1
[root@192 sbin]# date +%T

  1. 时间戳
1
[root@192 sbin]# date +%s

  1. 根据时间戳反向推测时间
1
[root@192 sbin]# date -d @1493232796

  1. 一天后
1
[root@192 sbin]# date -d "+1day"

  1. 一天前
1
[root@192 sbin]# date -d "-1day"

  1. 一月前
1
[root@192 sbin]# date -d "-1month"

  1. 一分钟前
1
[root@192 sbin]# date -d "-1min"

  1. 星期

        小写 w 表示周几

1
[root@192 sbin]# date +%w

        大写 W 表示本年的第几周

1
[root@192 sbin]# date +%W